<?php

namespace app\admin\model;

use think\Model;

/**
 * Designexample
 */
class Designexample extends Model
{
    // 表名
    protected $name = 'designexample';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    protected $append = [
        'domain_image',
        'create_time_text'
    ];
    public function getDomainImageAttr($value, $data)
    {
        return request()->domain().$data['image'];
    }
    public function getCreateTimeTextAttr($value,$data)
    {
        return date("Y/m/d",$data['create_time']);

    }

    protected static function onAfterInsert($model)
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }
    public function setCreateTimeAttr($value,$data){
        return !is_numeric($value)? strtotime($value) : $value;
    }

    public function getContentAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function furnish(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Furnish::class, 'furnish_id', 'id');
    }
}
